package pl.prv.polanie.client.widgets;

//import pl.prv.polanie.client.Widgets;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LabelUListBox extends Composite {
//	private static final String STYLE
	
	
	private SimplePanel mainSP = new SimplePanel(); 
	
	private ListBox dataLB = new ListBox();
	private TextBox dataTB = new TextBox();
	
	boolean hasWidgetFocus = false;
	
	boolean whileShowingLB = false;
	public LabelUListBox() {
		
		setupDataTB();
		setupDataLB();
		
		mainSP.setWidget(dataTB);
		
		
		
		initWidget(mainSP);
	}
	
	private void setupDataLB() {
		dataLB.addItem("pierwszy", "1");
		dataLB.addItem("drugi", "2");
		dataLB.addItem("trzeci", "3");
		
		dataLB.addFocusListener(new FocusListener() {
			public void onFocus(Widget sender) {
				//Widgets.log("LB onFocus");
				
			}
			public void onLostFocus(Widget sender) {
				if (whileShowingLB) {
					//Widgets.log("whileShowingLB = false");
					whileShowingLB = false;
					dataLB.setFocus(true);
				} else {
					//Widgets.log("LB onLostFocus");
					toggleView();
					dataTB.setText(getSelectedItemText());
				}
			}
		});
	}
	
	private void setupDataTB() {
		dataTB.addFocusListener(new FocusListener(){
			public void onFocus(Widget sender) {
				//Widgets.log("TB onFocus");
				toggleView();
				whileShowingLB = true;
				dataLB.setFocus(true);
				
			}
			public void onLostFocus(Widget sender) {
				
				//Widgets.log("TB onLostFocus");
			}
		});
	}
	

	
	private void toggleView() {
		if (hasWidgetFocus) {
			//chowamy ListBoxa
			hasWidgetFocus = false;
			mainSP.setWidget(dataTB);
		} else {
			//pokazujemy ListBoxa
			hasWidgetFocus = true;
			mainSP.setWidget(dataLB);
			//dataLB.setFocus(true);
		}
	}
	
	private void toggleViewToFocused() {
		hasWidgetFocus = true;
		mainSP.setWidget(dataLB);
	}
	
	private String getSelectedItemText() {
		return dataLB.getItemText(dataLB.getSelectedIndex());
	}

}
